
package com.yimixian.app.rest.api;

import android.text.TextUtils;

import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.Comment;
import com.yimixian.app.model.SearchKeyResult;

import org.json.JSONObject;

import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.client.Response;

/**
 * Created by songzy on 2015/10/26.
 */
public class CommentPublishAPI extends YmxBaseAPI<Comment> {

    public static final String COMMENT_PUBLISH = "/v5/commodity/comment/publish";

    private int mGoodId;
    private String mContent = "";
    private int mTaste;
    private int mFreshness;

    /**
     * @param callback
     */
    public CommentPublishAPI(int goodId, String content, int taste, int freshness,
            Callback<Response> callback) {
        super(callback);
        mGoodId = goodId;
        mContent = content;
        mTaste = taste;
        mFreshness = freshness;
    }

    @Override
    public String getRelativeURL() {
        return COMMENT_PUBLISH;
    }

    public void commentPublish() {
        String token = DataManager.getInstance().get(CacheKeys.TOKEN);
        if (TextUtils.isEmpty(token) || mGoodId == 0) {
            EventBus.getDefault().postSticky(new String(Constants.EVENT_BUS_DISSMISS_DIALOG));
            return;
        }
        int goodId = mGoodId;
        String content = mContent;
        int taste = mTaste;
        int freshness = mFreshness;
        mYmxRestService.commentPublish(token, goodId, content, taste, freshness, mCallback);
    }

    @Override
    public Comment parseJson(JSONObject jsonObject) {
        Comment comment = new Comment();
        if (jsonObject != null) {
            comment.id = jsonObject.optString("id");
            comment.goodId = jsonObject.optInt("good_id");
            comment.reviewerId = jsonObject.optInt("reviewer_id");
            comment.reviewerName = jsonObject.optString("reviewer_name");
            comment.content = jsonObject.optString("content");
            comment.tasteDesc = jsonObject.optString("taste_desc");
            comment.taste = jsonObject.optInt("taste");
            comment.freshnessDesc = jsonObject.optString("freshness_desc");
            comment.freshness = jsonObject.optInt("freshness");
        }
        return comment;
    }
}
